package com.littleHan.brushQuestions.utils;

import jdk.nashorn.internal.runtime.ParserException;

import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期排序
 * @author littleHan
 */
public class SortByDateUtil {


    /**
     * 根据
     * @param list
     * @return
     */
    public static List<String> sortDate(List<String> list) {
        List<String> data = list;
        System.out.println("排序前" + data);

        final Format f = new SimpleDateFormat("yyyy-MM-dd");
        Collections.sort(data, new Comparator<String>() {

            public int compare(String o1, String o2) {

                Date d1;
                Date d2;
                try {
                    //降序
                    d1 = (Date) f.parseObject(o2);
                    d2 = (Date) f.parseObject(o1);
                    //升序
                    /*d1 = (Date) f.parseObject(o1);
                    d2 = (Date) f.parseObject(o2);*/
                    return d1.compareTo(d2);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return 0;
            }
        });
        return data;
    }

    public static List<String> sortDateTime(List<String> list){
        List<String> data = list;
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        data.sort((a2,a1)->{
            try{
                return df.parse(a1).compareTo(df.parse(a2));
            }catch(ParseException e){
                e.printStackTrace();
            }
            return 0;
        });
        return data;
    }

}
